﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;

namespace WF.Beautiful
{
    public partial class BForm : Form
    {
        public BForm()
        {
            InitializeComponent();
        }

        private List<string> Llink = new List<string>();
        private A.HTMlServer.DCommon dc = new A.HTMlServer.DCommon();
        private object objState = new object();
        delegate void dfun();
        private string Url = null;

        A.HTMlServer.HTML.GetOnePage GoPage = new A.HTMlServer.HTML.GetOnePage();

        private void BForm_Load(object sender, EventArgs e)
        {
            //A.HTMlServer.HThread.ToThread.AddItem("123");
            //A.HTMlServer.HThread.ToThread.ToHtmlDelegate td = GoPage.GetBeautifulPageHtml;
            //A.HTMlServer.HThread.ToThread.ToHtmlDelegate td1 = new A.HTMlServer.HThread.ToThread.ToHtmlDelegate(GoPage.GetBeautifulPageHtml);
            //A.HTMlServer.HThread.ToThread.ToHtmlDelegate td2 = new A.HTMlServer.HThread.ToThread.ToHtmlDelegate(s => GoPage.GetBeautifulPageHtml(s));
        }
        private void Brun_Click(object sender, EventArgs e)
        {
            if (Brun.Text == "Run")
            {
                Url = TBurl.Text;
                if (Url == "")
                {
                    return;
                }
                //执行抓取链接
                //Thread Th = new Thread(GetLink);
                Thread Th = new Thread(ForUrl);
                Th.Start();

                Thread Tu = new Thread(ThreadUi);
                Tu.Start();
            }
            else
            {
                //执行内容
            }
            Brun.Enabled = false;
        }

        #region UI
        private void ThreadUi()
        {
            do
            {
                this.Invoke(new dfun(Go));
                Thread.Sleep(1000);
            } while (true);
        }

        private void Go()
        {
            if (Llink.Count > GoPage.J)
            {
                pBar.Maximum = Llink.Count;
                pBar.Value = GoPage.J;
            }
            Lmessage.Text = "下载完成" + GoPage.J + "/" + Llink.Count + "个";
            if (Llink.Count == GoPage.J)
            {
                Brun.Enabled = true;
            }
        }
        #endregion

        private void ForUrl()
        {
            //string[] slist = { "http://face.39.net/mrhf/hfjq/", "http://face.39.net/mrhf/hfdiy/" };
            string[] slist = { "http://face.39.net/mrhf/hfyd/bs/bscp/",
                                 "http://face.39.net/mrhf/hfyd/bs/bsfaq/",
                                 "http://face.39.net/mrhf/hfyd/bs/bsff/",
                                 "http://face.39.net/mrxf/mftj/",
                                 "http://face.39.net/mrxf/hfcs/",
                                 "http://face.39.net/mrxf/fxdiy/",
                                 "http://face.39.net/mrhf/hfyd/mb/",
                                 "http://face.39.net/mrhf/hfyd/fs/",
                                 "http://face.39.net/mrhf/hfjq/",
                                 "http://face.39.net/mrhf/hfdiy/"
                             };
            foreach(string s in slist)
            {
                Url=s;
                GetLink();
            }
        }

        private void GetLink()
        {
            //string url = "http://lengxiaohua.net/2008/12/11/time-machine/";
            //string Html = dc.GetStrHtml(url);
            //GetPageHtml(Html);http://face.39.net/mrhf/hfdiy/
            string url =Url;
            //string url = "http://face.39.net/mrhf/hfjq/"+Url;
            string Html = dc.GetStrHtml(url);
            if (Html != null)
            {
                int Count= GetCountPage(Html);
                for (var i = 1; i <= Count; i++)
                {
                    if (i != 1)
                    {
                        Html = dc.GetStrHtml(url + "index_" + i + ".html");
                    }
                    if (Html == null)
                    {
                        continue;
                    }
                    Html = dc.GetPartHtml(Html,"<div class=\"newslist\">(.*?)</div>\\s+<div class=\"page\">",1);
                    GetA(Html,@"http://face.39.net/\S+?/.*?\.html");
                    Thread.Sleep(50);
                }
            }
        }

        /// <summary>
        /// 获取页数
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        private int GetCountPage(string Html)
        {
            //下一页</a><a target="_self" href="index_97.html">尾页</a>
            string strPage = dc.GetPartHtml(Html, "下一页</a>\\s+<a.*?href=\"index_(\\d+).html\".*?>尾页</a>", 1);
            if (strPage != "")
            {
                return int.Parse(strPage);
            }
            return 0;
        }

        /// <summary>
        /// 获取所有文章链接
        /// </summary>
        /// <param name="Html"></param>
        private void GetA(string Html,string pattern)
        {
            List<string> Ltemp = new List<string>();
            Regex R = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection MC = R.Matches(Html);

            A.HTMlServer.HThread.ToThread.Tdelegate= GoPage.GetBeautifulPageHtml;

            foreach (Match m in MC)
            {
                if (!Llink.Contains(m.Value))
                {
                    Llink.Add(m.Value);
                    A.HTMlServer.HThread.ToThread.AddItem(m.Value);
                }
            }
        }
    }
}
